#include "editfileproperties_dialog.h"
#include "ui_editfileproperties_dialog.h"

EditFileProperties_Dialog::EditFileProperties_Dialog(SoundFile* pToSound, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::EditFileProperties_Dialog)
{
    ui->setupUi(this);

    curSound = pToSound;

    setWindowTitle( windowTitle() + curSound->getName());

    ui->commentaryField->setText( curSound->getCommentary());  // Комментарий
    ui->tagList->addItems(curSound->getTags());                // Теги

    for( int i = 0; i < ui->tagList->count(); i++)
    {
        ui->tagList->item(i)->setBackgroundColor(QColor("#B8E5FF"));
        ui->tagList->item(i)->setIcon(QIcon(":/icons/tag_icon.png"));

        ui->minusButton->setEnabled(true);
    }
}

void EditFileProperties_Dialog::on_saveButton_pressed()
{
    curSound->setCommentary( ui->commentaryField->text());

    curSound->clearTags();

    for( int i = 0; i < ui->tagList->count(); i++)
        curSound->addTag( ui->tagList->item(i)->text());

    /* ---- Получение списка тегов из реестра и добавление к ним отсутствующих--- */
    QStringList *tagList = new QStringList;
    QSettings *settings  = new QSettings("Cydeamon's Projects", "Gameloc");

    *tagList = settings->value("tags").toStringList();

    bool alreadyHave;
    for( int i = 0; i < curSound->getTags().count(); i++)
    {
        alreadyHave = false;

        for( int j = 0; j < tagList->count(); j++)
            if( tagList->at(j) == curSound->getTags().at(i))
                alreadyHave = true;

        if( !alreadyHave )
            tagList->push_back(curSound->getTags().at(i));
    }

    settings->setValue("tags", *tagList);

    curSound->updateToolTip();

    delete settings;
    delete tagList;

    deleteLater();
}

void EditFileProperties_Dialog::on_plusButton_pressed()
{
    addTagDialog = new addTag_dialog(this);
    addTagDialog->show();

    connect( addTagDialog, SIGNAL(tagReady()), SLOT(getTag()));
}

void EditFileProperties_Dialog::on_minusButton_pressed()
{
    QListWidgetItem *itemToDelete = ui->tagList->currentItem();

    itemToDelete->setHidden(true);
    delete itemToDelete;

    if( ui->tagList->count() == 0)
        ui->minusButton->setEnabled(false);
}

void EditFileProperties_Dialog::getTag()
{
    bool alreadyHave = false;

    for( int i = 0; i < ui->tagList->count(); i++)
        if( ui->tagList->item(i)->text() == addTagDialog->getTag())
            alreadyHave = true;

    if( alreadyHave )
        QMessageBox::information(this, "Тег уже есть", tr("Тег \"%1\" уже есть в списке").arg((addTagDialog->getTag())));
    else
    {
        ui->tagList->addItem( addTagDialog->getTag());
        ui->tagList->item( ui->tagList->count() - 1)->setBackgroundColor(QColor("#B8E5FF"));
        ui->tagList->item( ui->tagList->count() - 1)->setIcon(QIcon(":/icons/tag_icon.png"));
        ui->tagList->setCurrentItem( ui->tagList->item(ui->tagList->count() - 1));

        ui->minusButton->setEnabled(true);
    }
    addTagDialog->close();
    delete addTagDialog;
}

void EditFileProperties_Dialog::on_cancelButton_pressed()
{
    deleteLater();
}
EditFileProperties_Dialog::~EditFileProperties_Dialog()
{
    delete ui;
}
